package com.lihao.sam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lihao.sam.dao.MaintenanceStatisticalDao;
import com.lihao.sam.dto.MaintenanceStatisticalDto;
import com.lihao.sam.entity.MaintenanceStatistical;
import com.lihao.sam.service.intf.MaintenanceStatisticalService;
import com.lihao.sam.vo.PageVo;
import com.lihao.sam.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Qinheyun
 * @Description:TODO
 */
@Service
public class MaintenanceStatisticalServiceImpl implements MaintenanceStatisticalService {
    @Resource
    private MaintenanceStatisticalDao dao;

    @Override
    public R add(MaintenanceStatistical maintenanceStatistical) {
        if (dao.add(maintenanceStatistical)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R update(MaintenanceStatistical maintenanceStatistical) {
        if (dao.update(maintenanceStatistical)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R del(int id) {
        if (dao.del(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageVo all(String maintenanceUnits, String maintenanceName, int page, int limit) {
        //开启分页
        PageHelper.startPage(page,limit);
        //执行查询
        PageInfo<MaintenanceStatistical> pageInfo = new PageInfo<>(dao.all(maintenanceUnits,maintenanceName));
        //返回查询数据和总条数
        return PageVo.ok(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R querNum() {
        //1.查询
        List<MaintenanceStatisticalDto> list = dao.selectNum();
        //2.组装结果数据
        Map<String, ArrayList> map=new HashMap<>();
        map.put("maintenanceName",new ArrayList());
        map.put("num",new ArrayList());
        for(MaintenanceStatisticalDto ur:list){
            map.get("maintenanceName").add(ur.getMaintenanceName());
            map.get("num").add(ur.getNum());
        }
        return R.ok(map);
    }
}
